package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.entity.Employee;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    //登录功能
    @PostMapping("/login")//@RequestBody自动把前端传递的json转换成对象
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        //1、将页面提交的密码password进行md5加密处理
        String password = employee.getPassword();
        String passwords = DigestUtils.md5DigestAsHex(password.getBytes());
        //2、根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<Employee> elw = new LambdaQueryWrapper<>();
        elw.eq(Employee::getUsername, employee.getUsername());
        Employee one = employeeService.getOne(elw);
        //3、如果没有查询到则返回登录失败结果
        if (one==null){
            return R.error("用户名不存在");
        }
        //4、密码比对，如果不一致则返回登录失败结果
        if(!one.getPassword().equals(passwords)){
            return R.error("密码输入有误");
        }
        //5、查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if (one.getStatus()==0){
            return R.error("账号已被禁用，登录失败");
        }
        //6、登录成功，将员工id存入Session并返回登录成功结果
        request.getSession().setAttribute("employee",one.getId());
        return R.success(one);
    }

    /**
     * 员工信息分页查询
     * @param page 当前页码值
     * @param pageSize 每页展示记录数
     * @param name 员工姓名  可选参数
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //1).构造分页构造器
        Page page1 = new Page(page, pageSize);
        //2).构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //3).添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //4).添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        //5)执行查询
        employeeService.page(page1,queryWrapper);
        return R.success(page1);
    }

    //添加功能
    @PostMapping
    public R<String> save(@RequestBody Employee employee){
        log.info(String.valueOf(employee));
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employeeService.save(employee);
        return R.success("添加成功");
    }

    //更新
    @PutMapping
    public R<String> update(@RequestBody Employee employee){
        log.info(String.valueOf(employee));
        employeeService.updateById(employee);
        return R.success("员工信息更新成功");
    }
    //根据id查询
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        Employee byId = employeeService.getById(id);
        if (byId!=null){
            return R.success(byId);
        }
        return R.error("error");
    }
    //退出功能
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }
}
